using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace ProjectSurvivor
{
	public class UIGamePanelData : UIPanelData
	{
	}
	public partial class UIGamePanel : UIPanel
	{
		public static EasyEvent FlashScreen = new EasyEvent();
		
		public static EasyEvent OpenTreasurePanel = new EasyEvent();
		protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as UIGamePanelData ?? new UIGamePanelData();
			// please add init code here
			
			//记录当前所有的敌人数量
			EnemyGenerator.EnemyCount.RegisterWithInitValue(enemyCount =>
			{
				EnemyCountText.text = "敌人：" + enemyCount;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);;
			//记录游戏开始了多少时间
			Global.CurrentSeconds.RegisterWithInitValue(currentSeconds =>
			{
				//每30帧更新一次看着更顺畅
				if (Time.frameCount % 30 == 0)
				{
					var currentSecondsInt = Mathf.FloorToInt(currentSeconds);
					var seconds = currentSecondsInt % 60;
					var minutes = currentSecondsInt / 60;
					TimeText.text = "时间：" + $"{minutes:00}:{seconds:00}";
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			//注册值变更的一个事件,Exp值变化是更新Exp文本
			Global.Exp.RegisterWithInitValue(exp =>
			{
				ExpValue.fillAmount = exp / (float)Global.ExpToNextLevel();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);//当这个gmaebonjct销毁的时候进行事件的注销
			
			//等级变化时更新等级文本
			Global.Level.RegisterWithInitValue(lv =>
			{
				LevelText.text = "等级：" + lv;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			ExpUpgradePanel.Hide();
			//当等级发生变化时升级能力的两个按钮显示
			Global.Level.Register(lv =>
			{
				Time.timeScale = 0;
				ExpUpgradePanel.Show();
				AudioKit.PlaySound("LevelUp");
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			//Exp值发生变化时是否大于公式所算的值然后提升等级
			Global.Exp.RegisterWithInitValue(exp =>
			{
				if (exp >= 5 * Global.Level.Value)
				{ 
					Global.Exp.Value-=Global.ExpToNextLevel();
					Global.Level.Value++;
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			
			//判断游戏是否通关的逻辑
			var enemtGenerator = FindObjectOfType<EnemyGenerator>();
			ActionKit.OnUpdate.Register(() =>
			{
				Global.CurrentSeconds.Value += Time.deltaTime;
				if (enemtGenerator.LastWave && enemtGenerator.CurrentWave==null
				                            && EnemyGenerator.EnemyCount.Value==0)
				{
					this.CloseSelf();
					UIKit.OpenPanel<UIGamePassPanel>();
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			//金币值变化时的更新金币文本的注册
			Global.Coin.RegisterWithInitValue(coin =>
			{
				CoinText.text = "金币：" + coin;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			FlashScreen.Register(() =>
			{
				ActionKit
					.Sequence()
					.Lerp(0, 0.5f, 0.1f,
						alpha => ScreenColor.ColorAlpha(alpha))
					.Lerp(0.5f, 0, 0.2f,
						alpha => ScreenColor.ColorAlpha(alpha),
						() => ScreenColor.ColorAlpha(0))
					.Start(this);
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			OpenTreasurePanel.Register(() =>
			{
				Time.timeScale = 0.0f;
				TreasureChestPanel.Show();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}
		
		protected override void OnOpen(IUIData uiData = null)
		{
		}
		
		protected override void OnShow()
		{
		}
		
		protected override void OnHide()
		{
		}
		
		protected override void OnClose()
		{
		}
	}
}